package com.gitee.wsl.common.chart.sample.dial

import android.graphics.Canvas
import com.gitee.wsl.common.chart.ChartEnum
import com.gitee.wsl.common.chart.render.background.axis.round.RoundAxis
import com.gitee.wsl.common.chart.render.foreground.info.PlotAttrInfoRender
import com.gitee.wsl.common.chart.render.shape.pointer.Pointer
import com.gitee.wsl.common.chart.sample.AbstractCirChart
import com.gitee.wsl.common.ui.draw.type.Location

/**
 * @ClassName DialChart
 * @Description  仪表盘基类
 * @author XiongChuanLiang<br/>(xcl_168@aliyun.com)
 *
 */

class DialChart:AbstractCirChart() {

     var startAngle = INIT_ANGLE
     var totalAngle = FIX_TOTAL_ANGLE

     val roundAxisList = mutableListOf<RoundAxis>()

    //指针[默认1个，其它add]
    val pointer: Pointer = Pointer()
    var pointerSet = mutableListOf<Pointer>()

    //附加信息类
    val plotAttrInfoRender: PlotAttrInfoRender = PlotAttrInfoRender()

    override fun getType(): ChartEnum.ChartType {
        return ChartEnum.ChartType.DIAL
    }

    /**
     * 设置饼图(pie chart)起始偏移角度
     * @param Angle 偏移角度
     */
    fun setInitialAngle(Angle: Float) {
        offsetAngle = Angle
        initOffsetAngle = offsetAngle
    }


    /**
     * 清掉所有相关信息
     */
    fun clearAll() {
        pointerSet.clear()
        roundAxisList.clear()
        plotAttrInfoRender.clearPlotAttrInfo()
    }

    /**
     * 绘制指针
     * @param canvas    画布
     */
    private fun drawPointerLine(canvas: Canvas) {
        for (i in pointerSet.indices) {
            val pointer = pointerSet[i]
            pointer.setParentRadius(radius)
            pointer.setStartXY(plotViewport.centerX, plotViewport.centerY)
            pointer.setTotalAngle(totalAngle)
            pointer.setStartAngle(startAngle)
            pointer.render(canvas)
        }
        pointer.setStartXY(plotViewport.centerX, plotViewport.centerY)
        pointer.setTotalAngle(totalAngle)
        pointer.setStartAngle(startAngle)
        pointer.setParentRadius(radius)
        pointer.render(canvas)
    }


    /**
     * 增加 标签环形轴
     * @param radiusPercentage 显示在总半径的指定比例所在位置
     * @param labels 标签集合
     */
    fun addInnerTicksAxis(radiusPercentage: Float, labels: List<String>) {
        addTicksAxis(radiusPercentage, labels, ChartEnum.RoundTickAxisType.INNER_TICKAXIS)
    }

    /**
     * 增加 标签环形轴
     * @param radiusPercentage 显示在总半径的指定比例所在位置
     * @param labels 标签集合
     */
    fun addOuterTicksAxis(radiusPercentage: Float, labels: List<String>) {
        addTicksAxis(radiusPercentage, labels, ChartEnum.RoundTickAxisType.OUTER_TICKAXIS)
    }


    private fun addTicksAxis(
        radiusPercentage: Float,
        labels: List<String>, type: ChartEnum.RoundTickAxisType
    ) {
        val roundAxis = RoundAxis()
        roundAxis.setRoundAxisType(ChartEnum.RoundAxisType.TICKAXIS)
        roundAxis.radiusPercentage=radiusPercentage
        roundAxis.setAxisLabels(labels)
        roundAxis.roundTickAxisType=type
        this.roundAxisList.add(roundAxis)
    }


    /**
     * 增加空心环形轴集合
     * @param outerRadiusPercentage    外环显示在总半径的指定比例所在位置
     * @param innerRadiusPercentage    内环显示在总半径的指定比例所在位置
     * @param percentage    百分比
     * @param color    颜色
     */
    private fun addStrokeRingAxis(
        outerRadiusPercentage: Float, innerRadiusPercentage: Float,
        percentage: List<Float>, color: List<Int>
    ) {
        addRingAxis(outerRadiusPercentage, innerRadiusPercentage, percentage, color, emptyList())
    }

    /**
     * 增加空心环形轴集合
     * @param outerRadiusPercentage    外环显示在总半径的指定比例所在位置
     * @param innerRadiusPercentage    内环显示在总半径的指定比例所在位置
     * @param percentage    百分比
     * @param color    产色
     * @param labels    标签集合
     */
    private fun addStrokeRingAxis(
        outerRadiusPercentage: Float,
        innerRadiusPercentage: Float,
        percentage: List<Float>,
        color: List<Int>,
        labels: List<String>
    ) {
        addRingAxis(outerRadiusPercentage, innerRadiusPercentage, percentage, color, labels)
    }


    /**
     * 增加实心环形轴集合
     * @param radiusPercentage    显示在总半径的指定比例所在位置
     * @param percentage    百分比
     * @param color    颜色
     */
    private fun addFillRingAxis(
        radiusPercentage: Float,
        percentage: List<Float>, color: List<Int>
    ) {
        addRingAxis(radiusPercentage, 0.0f, percentage, color, emptyList())
    }

    /**
     * 增加实心环形轴集合
     * @param radiusPercentage 显示在总半径的指定比例所在位置
     * @param percentage    百分比
     * @param color    颜色
     * @param labels    标签集合
     */
    private fun addFillRingAxis(
        radiusPercentage: Float,
        percentage: List<Float>,
        color: List<Int>, labels: List<String>
    ) {
        addRingAxis(radiusPercentage, 0.0f, percentage, color, labels)
    }


    /**
     * 增加环形轴集合
     * @param outerRadiusPercentage    外环显示在总半径的指定比例所在位置
     * @param innerRadiusPercentage    内环显示在总半径的指定比例所在位置
     * @param percentage    百分比
     * @param color    颜色
     * @param labels    标签集合
     */
    private fun addRingAxis(
        outerRadiusPercentage: Float, innerRadiusPercentage: Float,
        percentage: List<Float>,
        color: List<Int>, labels: List<String>
    ) {
        val roundAxis = RoundAxis()
        roundAxis.setRoundAxisType(ChartEnum.RoundAxisType.RINGAXIS)
        roundAxis.radiusPercentage=outerRadiusPercentage
        roundAxis.innerRadiusPercentage = innerRadiusPercentage
        roundAxis.setAxisPercentage(percentage)
        roundAxis.setAxisColor(color)
        roundAxis.setAxisLabels(labels)
        this.roundAxisList.add(roundAxis)
    }

    /**
     * 增加弧线轴
     * @param radiusPercentage    显示在总半径的指定比例所在位置
     */
    private fun addArcLineAxis(radiusPercentage: Float) {
        val roundAxis = RoundAxis()
        roundAxis.setRoundAxisType(ChartEnum.RoundAxisType.ARCLINEAXIS)
        roundAxis.radiusPercentage=radiusPercentage
        this.roundAxisList.add(roundAxis)
    }

    /**
     * 增加填充环形轴
     * @param radiusPercentage    显示在总半径的指定比例所在位置
     * @param color    颜色
     */
    private fun addFillAxis(radiusPercentage: Float, color: Int) {
        val roundAxis = RoundAxis()
        roundAxis.setRoundAxisType(ChartEnum.RoundAxisType.FILLAXIS)
        roundAxis.radiusPercentage=radiusPercentage
        val lstColor: MutableList<Int> = ArrayList()
        lstColor.add(color)
        roundAxis.setAxisColor(lstColor)
        this.roundAxisList.add(roundAxis)
    }


    /**
     * 增加填充圆轴
     * @param radiusPercentage    显示在总半径的指定比例所在位置
     * @param color    颜色
     */
    private fun addCircleAxis(radiusPercentage: Float, color: Int) {
        val roundAxis = RoundAxis()
        roundAxis.setRoundAxisType(ChartEnum.RoundAxisType.CIRCLEAXIS)
        roundAxis.radiusPercentage=radiusPercentage
        val lstColor: MutableList<Int> = ArrayList()
        lstColor.add(color)
        roundAxis.setAxisColor(lstColor)
        this.roundAxisList.add(roundAxis)
    }


    /**
     * 增加线轴
     * @param location            位置
     * @param radiusPercentage  占总半径的指定比例
     */
    private fun addLineAxis(
        location: Location,
        radiusPercentage: Float
    ) //ChartEnum.LineStyle lineStyle,ChartEnum.DotStyle dotStyle) 
    {
        val roundAxis = RoundAxis()
        roundAxis.setRoundAxisType(ChartEnum.RoundAxisType.LINEAXIS)
        roundAxis.radiusPercentage=radiusPercentage
        roundAxis.location=location

        //没心情搞了，不弄这个线类型及箭头啥的了
        //roundAxis.setLineStyle(lineStyle);
        //roundAxis.setLineCap(dotStyle);
        this.roundAxisList.add(roundAxis)
    }


    /**
     * 绘制图
     */
    protected fun drawPlot(canvas: Canvas) {
            //画上各组环形轴
            for (i in roundAxisList.indices) {
                val roundAxis = roundAxisList[i] as RoundAxis
                roundAxis.setCenterXY(plotViewport.centerX, plotViewport.centerY)
                roundAxis.setAngleInfo(totalAngle, startAngle)
                roundAxis.setOrgRadius(radius)
                roundAxis.render(canvas)
            }

            //绘制附加信息
            plotAttrInfoRender.renderAttrInfo(canvas, plotViewport.centerX, plotViewport.centerY, radius)

            //最后再画指针
            drawPointerLine(canvas)
            roundAxisList.clear()
            pointerSet.clear()
            plotAttrInfoRender.clearPlotAttrInfo()
    }


    override fun renderBody(canvas: Canvas): Boolean {
         super.renderBody(canvas)
          //绘制图表
         drawPlot(canvas)
         return true
    }

    companion object{
        private const val INIT_ANGLE = 135f
        private const val FIX_TOTAL_ANGLE = 270f
    }
}